/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.caracol.crypto;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
import sun.misc.CharacterEncoder;

/**
 * This class is responsable for ecrypt the given texto to
 * his corresponding hash algoritm
 * @author Roquet
 */
public final class SingletonPassword {

    private static SingletonPassword instance;

    private SingletonPassword() {
    }

    public synchronized String encrypt(String plaintext) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(plaintext.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }

    public static synchronized SingletonPassword getInstance() {
        if (instance == null) {
            instance = new SingletonPassword();
        }
        return instance;
    }
}